package com.atguigu.study.base.SaleTick;

import java.util.concurrent.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class Tickt //资源类 对象 = Field + Method
{
    private int number = 50;
   /* public synchronized void sale(){
         if(number>0){
             System.out.println(Thread.currentThread().getName()+"卖出第"+(51-number--)+"张票 " + "还剩"+(number));
         }
        try {
            Thread.sleep(200);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }*/


    private final Lock lock = new ReentrantLock(true); //ReentrantLock 叫可重入锁 / 悲观锁/ 默认是非公平锁

    public void sale() {
        lock.lock();
        try {
            if (number > 0) {
                System.out.println(Thread.currentThread().getName() + "卖出第" + (51 - number--) + "张票 " + "还剩" + (number));
            }
        } finally {

            lock.unlock();

        }


    }


}


/**
 * @Author 快点干早点散
 * @Create 2025/4/22 18:49
 * Description:
 */
public class SaleTickDemo {
    public static void main(String[] args) {

        //Thread(Runnable target, String name) Allocates a new Thread object.

        Tickt tickt = new Tickt();

       /* //v1
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    tickt.sale();
                }
            }
        },"A").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    tickt.sale();
                }
            }
        },"B").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 50; i++) {
                    tickt.sale();
                }
            }
        },"C").start();
*/
        //v2
//        new Thread(() -> {
//            for (int i = 0; i < 50; i++) tickt.sale();
//        }, "A").start();
//        new Thread(() -> {
//            for (int i = 0; i < 50; i++) tickt.sale();
//        }, "B").start();
//        new Thread(() -> {
//            for (int i = 0; i < 50; i++) tickt.sale();
//        }, "C").start();
        //v3线程池
//        ExecutorService threadPool=Executors.newFixedThreadPool(3);
        ExecutorService executorService = new ThreadPoolExecutor(
                3,//Runtime.getRuntime().availableProcessors(),
                3,//Runtime.getRuntime().availableProcessors() + 1,
                1L,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(50-3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy()
        );
        try {
            for (int i =1; i <=50; i++) {
                executorService.execute(()->{
                    tickt.sale();
                });
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            executorService.shutdown();
        }

//        try {
//            for (int i = 1; i <=50; i++) {
//                threadPool.execute(()->{
//                    tickt.sale();
//                });
//            }
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        } finally {
//            threadPool.shutdown();
//        }

    }
}
